package com.dc.business.datacollectbusiness.controller;

import com.dc.business.datacollectbusiness.entity.Line;
import com.dc.business.datacollectbusiness.model.Result;
import com.dc.business.datacollectbusiness.service.LineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/line")
public class LineController {
    @Autowired
    private LineService lineService;
    @GetMapping("/{workShopId}")
    public Result getAllLineByWorkShopId(@PathVariable("workShopId") Integer workShopId) {
        List<Line> lineList=lineService.getAllLinesByWokShopId(workShopId);
        if(lineList.isEmpty()){
            return Result.error("line not found");
        }else {
            return Result.success(lineList);
        }
    }
    @PostMapping("/addLine")
    public Result addLine(@RequestBody Line line) {
        try {
            lineService.add(line);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
    @PutMapping("/updateLine")
    public Result updateLine(@RequestBody Line line) {
        try {
            lineService.update(line);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
    @DeleteMapping("/{lineId}")
    public Result deleteLine(@PathVariable("lineId") Integer lineId) {
        try {
            lineService.delete(lineId);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}
